package il.technion.cs236369.parsers;

import static junit.framework.Assert.assertEquals;
import il.technion.cs236369.exceptions.ParsingException;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;

public class XMLToJsonTest {

	private final File	testFile	= new File("TestFiles/test.xml");
	XMLToJson parser = new XMLToJson();
	
	@Test(expected = IllegalArgumentException.class)
	public void testExcepionNullStream() throws Exception {
		InputStream stream = null;
		parser.parse(stream);
	}

	@Test(expected = IllegalArgumentException.class)
	public void testExcepionNullFile() throws Exception {
		File file = null;
		parser.parse(file);
	}

	@Test(expected = FileNotFoundException.class)
	public void testExcepionNotFound() throws Exception {
		parser.parse(new File("C:/DoesNotExist.really"));
	}

	@Test(expected = ParsingException.class)
	public void testExcepionBadFormat() throws Exception {
		parser.parse(new ByteArrayInputStream("Bla bla".getBytes()));
	}

	@Test
	public void testXMLRead() throws Exception {
		JSONObject result = parser.parse(testFile);
		JSONObject songs = result.getJSONObject("songs");
		JSONArray songsArray = songs.getJSONArray("songArray");
		assertEquals(2, songsArray.length());
		JSONObject firstSong = songsArray.getJSONObject(0);
		assertEquals("Rock", firstSong.get("-style"));
		assertEquals("Foxy Shazam", firstSong.getJSONObject("artist").get("#text"));
		assertEquals("Oh Lord", firstSong.getJSONObject("title").get("#text"));
		JSONObject secondSong = songsArray.getJSONObject(1);
		assertEquals("lida", secondSong.get("-kuki"));
		assertEquals("Rolling Stones", secondSong.getJSONObject("artist").get("#text"));
		assertEquals("Under my Thumb", secondSong.getJSONObject("title").get("#text"));
		JSONObject piece = songs.getJSONObject("piece");
		assertEquals("Beethoven", piece.getJSONObject("composer").get("#text"));
		assertEquals("Symphony no. 9", piece.getJSONObject("title").get("#text"));
		
	}
}
